package cn.kai.org.controller;

import cn.kai.basic.anno.PreAuthorize;
import cn.kai.basic.util.AjaxResult;
import cn.kai.basic.util.PageList;
import cn.kai.org.query.DepartmentQuery;
import cn.kai.org.domain.Department;
import cn.kai.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Kai
 * @Date 2022/6/5 12:42
 * @PackageName:cn.kai.org.controller
 * @ClassName: DepartmentController
 * @Description: depaertment表controller层
 * @Version 1.0
 */

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    /**
     * 基于id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Department queryById(@PathVariable("id") Long id){
        return departmentService.queryById(id);
    }

    /**
     * 查询所有数据
     * @return
     */
    @GetMapping
    public List<Department> queryAll(){
        return departmentService.queryAll();
    }

    /**
     * 高级查询及分页
     * @param query
     * @return
     */
    @PostMapping
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query){
        return departmentService.queryPage(query);
    }

    /**
     * 删除数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除部门",value = "department:delete")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            departmentService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("服务器繁忙，请稍后重试！");
        }
    }

    /**
     * 新增或修改
     * @param department
     * @return
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if (department.getId() == null){
                departmentService.save(department);
            }else {
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("服务器繁忙，请稍后重试！");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            departmentService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("服务器繁忙，请稍后重试！");
        }
    }

    /**
     * 获取部门树
     * @return
     */
    @GetMapping("/tree")
    public List<Department> queryTree(){
        return departmentService.queryTree();
    }
}
